package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.InformationDto;
import com.huanghuai.retail.entity.*;
import com.huanghuai.retail.mapper.InformationMapper;
import com.huanghuai.retail.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, PInformation> implements InformationService {
    @Autowired
    private InformationService informationService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private FactoryService factoryService;

    @Autowired
    private BrandTypeService brandTypeService;


    /**
     * 查询商品信息，并封装到dto中
     * @return
     */
    @Override
    public List<InformationDto> findInformationDto() {
        List<PInformation> informationList = informationService.list();
        if (informationList == null)
            return null;
        List<InformationDto> informationDtoList = new ArrayList<>();

        for(int i = 0; i < informationList.size(); i ++){
            PInformation information = informationList.get(i);

            InformationDto informationDto = informationService.disposeDto(information);

            informationDtoList.add(informationDto);
        }

        return informationDtoList;
    }

    @Override
    public InformationDto disposeDto(PInformation information) {
        InformationDto informationDto = new InformationDto();
        //copy基本信息
        BeanUtils.copyProperties(information, informationDto);

        //处理商品类型信息
        Long typeId = information.getTypeId();
        PType pType = typeService.getById(typeId);
        informationDto.setTypeName(pType.getNameTwo());

        //处理工厂信息
        Long factoryId = information.getFactoryId();
        Factory factory = factoryService.getById(factoryId);
        informationDto.setFactoryName(factory.getName());

        //处理品牌信息
        Long brandId = information.getBrandId();
        PBrand brand = brandService.getById(brandId);
        informationDto.setBrandName(brand.getName());

        return informationDto;
    }

    /**
     * 根据商品信息维护品牌类别表
     * @param information
     */
    @Override
    public void maintenance(PInformation information) {
        Long brandId = information.getBrandId();
        Long typeId = information.getTypeId();
        PBrandType pBrandType = new PBrandType();
        pBrandType.setBrandId(brandId);
        pBrandType.setTypeId(typeId);

        LambdaQueryWrapper<PBrandType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PBrandType::getBrandId, brandId);
        lambdaQueryWrapper.eq(PBrandType::getTypeId, typeId);
        PBrandType brandType = brandTypeService.getOne(lambdaQueryWrapper);
        //判断是否以及存在，如果不存在，则新增
        if (brandType == null)
            brandTypeService.save(pBrandType);
    }

    @Override
    public boolean pdInformation(PInformation information) {
        if (information.getName() == null || information.getTypeId() == null ||
            information.getFactoryId() == null || information.getBrandId() == null ||
            information.getWrap() == null || information.getPrice() == null)
            return false;

        if(factoryService.getById(information.getFactoryId()) == null ||
            brandService.getById(information.getFactoryId()) == null ||
            typeService.getById(information.getTypeId()) == null)
            return false;

        return true;
    }

    @Override
    public void pdDelBrandType(PInformation information) {
        information = informationService.getById(information.getId());
        Long brandId = information.getBrandId();
        Long typeId = information.getTypeId();
        LambdaQueryWrapper<PInformation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PInformation::getTypeId, typeId);
        lambdaQueryWrapper.eq(PInformation::getBrandId, brandId);
        List<PInformation> list = informationService.list(lambdaQueryWrapper);
        if (list.size() > 1)
            return;

        //删除
        LambdaQueryWrapper<PBrandType> brandTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        brandTypeLambdaQueryWrapper.eq(PBrandType::getTypeId, typeId);
        brandTypeLambdaQueryWrapper.eq(PBrandType::getBrandId, brandId);
        brandTypeService.remove(brandTypeLambdaQueryWrapper);
    }
}
